1283E - New Year Parties - CodeForces Solution


dp greedy *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define AM17 ios_base::sync_with_stdio(false),cin.tie(NULL);
#define yes cout<<"YES\n";
#define no cout<<"NO\n";

#define lenSorts(s) sort(s.begin(), s.end(), [&] (const string &s, const string &t) { return s.size() < t.size();});
int dx4[4] = { -1, 0, 1, 0 };
int dy4[4] = { 0, 1, 0, -1 };
int dx8[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy8[] = {-1, +1, +0, +0, +1, -1, +1, -1};
string Dir = "URDL";
template <class T>
istream & operator>> (istream&is , vector<T> &v )
{
    for (auto &i:v)
        is>> i ;
    return is ;
}
template <class T>
ostream & operator<< (ostream&os ,const vector<T> &v )
{
    for (auto &i:v)
        os << i << " " ;
    os << '\n' ;
    return os ;
}
ll MOD;
ll fastpow(ll a,ll p)
{
    if(p==0)
        return 1ll;

    if(p&1)
        return ((a%MOD) * fastpow(a,p-1))%MOD;
    else
    {
        ll ret=(fastpow(a,p/2));
        return ((ret%MOD) * (ret%MOD))%MOD;
    }
}
//
////ll w[N];
const int N=2e5+10;

//int parent[N];
//struct DSU{
////    vector<ll>cost;
////    ll mx=0;
//    DSU(int n)
//    {
////        sz.resize(N);
//        for (int i = 0; i <= n ; ++i) {
//            parent[i]=i;
////            sz[i]=1;
//        }
//    }
//    int find(int node)
//    {
//        return parent[node];
////        if(parent[node]==node)
////            return parent[node];
////        return parent[node]= find(parent[node]);
//    }
//    bool add(int a,int b)
//    {
////        a= find(a);
////        b= find(b);
////        if(a==b)
////            return false;
////        if(sz[a]<sz[b])
////        { swap(a,b);}
////        parent[b]=a;
////        sz[a]+=sz[b];
//        parent[a]=parent[b];
//        return true;
//    }
//};
//
int main()
{
    int loop=1;
//    cin>>loop;
    while (loop--)
    {
        int n;
        cin>>n;
        vector<int>v(n);
        cin>>v;
        vector<bool>vis(2e5+5, false);
        int mn=0,mx=1;
        sort(v.begin(),v.end());

        for (int i = 0; i < n; ++i) {
            for (int j = -1; j <2 ; ++j) {
                if(!vis[v[i]+j])
                {
                    vis[v[i]+j]=true;
                    mn++;
                    break;
                }
            }
        }
        for (int i = 1; i < n; ++i) {
            if(v[i]-v[i-1]<=2)
            {v[i]=v[i-1];continue;}

            mx++;
        }
        cout<<mx<<" "<<mn<<endl;


    }
}


Comments

Submit
0 Comments
More Questions

516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II